package com.jiayang.hhotel.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.CheckOutItemAdapter;
import com.jiayang.hhotel.bean.CheckOutMethodModel;
import com.jiayang.hhotel.bean.CheckOutModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.FinanceHandleModel;
import com.jiayang.hhotel.presenter.CheckOutTabPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XLazyFragment;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 结账退房子fragment 传递参数用于判断到底是来自那种item的请求
 */

public class CheckOutTabFragment extends XLazyFragment<CheckOutTabPresenter> implements View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private SwipeRefreshLayout fresh;
    private SwipeMenuRecyclerView recyclerView;

    private int flag = -1;//账单类型：0自付账单 1公付账单
    private long mpRegister = -1;
    private long mpGstAcct = -1;

    private CheckOutItemAdapter adapter;
    private int page = 1;

    private FinanceHandleModel.PCashierListDtosBean model;

    public CheckOutTabFragment(int flag,FinanceHandleModel.PCashierListDtosBean model){
        this.flag = flag;
        this.model = model;
    }
    //当前页面请求是否成功
    public boolean IsSuccess(){
        if(adapter != null){
            if(adapter.getItemCount()>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        fresh= (SwipeRefreshLayout) getRootView().findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) getRootView().findViewById(R.id.recyclerView);

        layout = getRootView().findViewById(R.id.ll_data);
        icon = (ImageView) getRootView().findViewById(R.id.iv_pic);
        tv = (TextView) getRootView().findViewById(R.id.tv_text);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        Button jieZhang= (Button) getRootView().findViewById(R.id.bt_jiezhang);

        jieZhang.setOnClickListener(this);

        isLoadMore = true;
        isRefresh(true);
        getCheckOutList();
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page = 1;
                    getCheckOutList();
                }else{
                    isRefresh(false);
                }
            }
        });

//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
//                    if (!isLoadMore) {
//                        page++;
//                        getCheckOutList();
//                    }
//                }
//            }
//        });
    }
    public void isRefresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    public void setAdapter(List<CheckOutModel.PTransDetailListDtosBean> model){
        isRefresh(false);
        isLoadMore = false;
        if(adapter==null){
            adapter=new CheckOutItemAdapter(getActivity(),model);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    public CheckOutItemAdapter getAdapter(){
        return adapter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_check_out;
    }

    @Override
    public CheckOutTabPresenter newP() {
        return new CheckOutTabPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_jiezhang://结账列表
                if(adapter != null){
                    int mpMark = -1;
                    if(adapter.getAmount()<0){//金额小于0
                        if(flag == 1){
                            mpMark = 0;
                        }else{
                            mpMark = 1;
                        }
                        mpGstAcct = model.getGstAcct();
                        mpRegister = model.getRegister();
                    }
                    CommonUtil.showProgressDialog(getActivity(),R.string.loading);
                    getP().asyncCheckOutMethodListLoad(Constant.LAUGUAGE_FLAG_CHINESE,1,mpRegister,mpGstAcct,mpMark,adapter.getAmount());
                }
                break;
        }
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isRefresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            fresh.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(Constant.NO_DATA_PICTURE);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(Constant.NET_ERROR_PICTURE);
                    tv.setText(getString(R.string.service_error_contract_manager));
                    icon.setOnClickListener(null);
                    break;
                default://其他错误
                    icon.setImageResource(Constant.TIME_OUT_PICTURE);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fresh.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isRefresh(true);
                            getCheckOutList();
                        }
                    });
                    break;
            }
        }else{
            ToastUtil.Toast(StringUtil.errMsg(state),getActivity());
        }
    }
    //初始化结账方式列表对话框
    public void initCheckOutMethodDialog(List<CheckOutMethodModel.PCashierPayListDtosBean> list){
        CommonUtil.showProgressDialog(getActivity(),R.string.loading);
        getP().checkOutMethodDialog(getActivity(),list);
    }
    //获取结账方式列表
    public void saveCheckOutMethod(CheckOutMethodModel.PCashierPayListDtosBean model){
        CommonUtil.showProgressDialog(getActivity(),R.string.loading);
        getP().asyncSaveCheckOutMethodListLoad(Constant.LAUGUAGE_FLAG_CHINESE,flag==1?1:2,this.model.getGstAcct(),model.getType(),getAdapter().getAmount(),getAdapter().getMpSerial());
    }
    //获取结账列表
    public void getCheckOutList(){
        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getGstAcct(),flag);
    }
}
